package com.shhic.hhplt.server.biz;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import com.shhic.hhplt.server.exceptions.BoxNumberParamException;
import com.shhic.hhplt.server.exceptions.CodeExhaustException;
import com.shhic.hhplt.server.exceptions.NoBoxNumberException;
import com.shhic.hhplt.server.exceptions.PatternMismatchException;
import com.shhic.hhplt.server.model.CommonRtnString;
import com.shhic.hhplt.server.service.IBoxNumberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.shhic.hhplt.server.model.BoxNumber;

@Component
public class BoxNumberMaintaince implements IBoxNumberMaintaince{
	@Autowired
	IBoxNumberService bnService;
	
	private Map<String,IBoxNumberGeneraterContainer> scContainerMap = new HashMap<String, IBoxNumberGeneraterContainer>();
	
	@PostConstruct
	public void loadAllSerialCode(){
		Set<BoxNumber> allBoxNumber = bnService.getAllBoxNumber();
		synchronized(scContainerMap){
			for(BoxNumber s:allBoxNumber){
				if(s.isAvailable()){
					try {
						scContainerMap.put(s.getCodeNameTitle(),new BoxNumberGeneraterContainer(s));
					} catch (PatternMismatchException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	@Override
	public CommonRtnString generateBoxNumber(String key) throws CodeExhaustException, NoBoxNumberException, BoxNumberParamException {
		IBoxNumberGeneraterContainer boxNumberGeneraterContainer ;
		synchronized(scContainerMap){
			if(!scContainerMap.containsKey(key))throw new NoBoxNumberException();
			boxNumberGeneraterContainer = scContainerMap.get(key);
		}
		return updateBoxNumber(key,boxNumberGeneraterContainer);
	}

	private CommonRtnString updateBoxNumber(String codeName,IBoxNumberGeneraterContainer boxNumberGeneraterContainer ) throws BoxNumberParamException, NoBoxNumberException, CodeExhaustException {
		//通过编码名称获取实体属性
		Set<BoxNumber> allBoxNumber = bnService.getAllBoxNumber();
		if(allBoxNumber.size()==0){
			throw new NoBoxNumberException();
		}else{
			for (BoxNumber boxNumber : allBoxNumber) {
				if(boxNumber.getCodeName().equals(codeName)){
					updateBoxNumber(boxNumber,boxNumberGeneraterContainer );
					return new CommonRtnString(String.valueOf(boxNumber.getCount()),String.valueOf(boxNumber.getCaseCode()),String.valueOf(boxNumber.getNowCode()));
				}
			}
		}
		return null;
	}

	private void updateBoxNumber(BoxNumber boxNumber,IBoxNumberGeneraterContainer boxNumberGeneraterContainer ) throws BoxNumberParamException, CodeExhaustException {
		int isAvailable=boxNumber.getIsAvailable();
		if(isAvailable==1&&boxNumber.getQuantity()!=null&&boxNumber.getCaseCode()!=null&&boxNumber.getNowCode()!=null
				&&boxNumber.getScheme()!=null&&boxNumber.getCount()!= null){
			int quantity = boxNumber.getQuantity(); //需要获取箱子容量
			int cpcCount=boxNumber.getCount();       //当前此镭雕cpc数量
			String nowCode=boxNumber.getNowCode();   //当前编码
			int caseCode=boxNumber.getCaseCode();   //当前箱子数目
			//需要获取当前箱号,末尾四位
			cpcCount++;
			if(cpcCount==0){
				caseCode=0;
			}
			if(cpcCount%quantity==1){
				nowCode=boxNumberGeneraterContainer.getAndGenerateCode();
				caseCode++;
			}
			boxNumber.setCount(cpcCount);
			boxNumber.setCaseCode(caseCode);
			boxNumber.setNowCode(nowCode);
			bnService.insertOrUpdateBoxNumber(boxNumber);
		}else {
			throw new BoxNumberParamException();
		}

	}
	@Override
	public CommonRtnString getCount(String codeName) throws NoBoxNumberException {
		Set<BoxNumber> allBoxNumber = bnService.getAllBoxNumber();
		if(allBoxNumber.size()==0){
			throw new NoBoxNumberException();
		}else{
			for (BoxNumber boxNumber : allBoxNumber) {
				if(boxNumber.getCodeName().equals(codeName)){
					return new CommonRtnString(String.valueOf(boxNumber.getCount()));
				}
			}
		}
		throw new NoBoxNumberException();
	}
	@Override
	public void notifyDeletion(String codeName) {
		synchronized(scContainerMap){
			BoxNumber sc = new BoxNumber();
			sc.setCodeName(codeName);
			String codeNameTitle = sc.getCodeNameTitle();
			if(scContainerMap.containsKey(codeNameTitle) &&
					scContainerMap.get(codeNameTitle).getCodeName().equals(codeName)){
				scContainerMap.remove(codeNameTitle);
			}
		}
	}
	
	@Override
	public void updateBoxNumber(BoxNumber bn) throws PatternMismatchException {
		synchronized(scContainerMap){
			scContainerMap.remove(bn.getCodeNameTitle());
			if(bn.isAvailable()){
				scContainerMap.put(bn.getCodeNameTitle(),new BoxNumberGeneraterContainer(bn));
			}
		}
	}
	
	@Override
	public void retrieveBoxNumber(String codeName, String code) throws NoBoxNumberException {
		IBoxNumberGeneraterContainer boxNumberGeneraterContainer ;
		synchronized(scContainerMap){
			if(!scContainerMap.containsKey(codeName))throw new NoBoxNumberException();
			boxNumberGeneraterContainer = scContainerMap.get(codeName);
			boxNumberGeneraterContainer.retrieveCode(code);
		}
		bnService.addRetrivedCode(codeName, code);
	}
}
